﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoBackupDatabase.DBConection;
using System.Data;
using System.Data.SqlClient;
using AutoBackupDatabase.ENT;

namespace AutoBackupDatabase.DAL
{
    public class ROLEDAL
    {
        private DbConnection conn;
        public ROLEDAL()
        {
            conn = new DbConnection();
        }
        public List<ROLEENT> GET_ROLE_ALL()
        {
            string query = string.Format("exec GET_ROLE_ALL ");
            DataTable ds = conn.executeGetQuery(query);
            List<ROLEENT> List = new List<ROLEENT>();
            foreach (DataRow item in ds.Rows)
            {
                ROLEENT ROLEent = new ROLEENT();
                ROLEent.ROLECODE = item["ROLECODE"].ToString();
                ROLEent.ROLENAME = item["ROLENAME"].ToString();
                ROLEent.GROUPROLECODE = item["GROUPROLECODE"].ToString();
                ROLEent.USERCREATE = item["USERCREATE"].ToString();
                ROLEent.DATECREATE = DateTime.Parse(item["DATECREATE"].ToString());
                ROLEent.USERUPDATE = item["USERUPDATE"].ToString();
                ROLEent.LASTUPDATE = DateTime.Parse(item["LASTUPDATE"].ToString());
                List.Add(ROLEent);
            }

            return List;
        }


        public List<ROLEENT> GET_ROLE_PERMITSION(GROUPUSERENT ent)
        {
            string query = string.Format("exec GET_ROLE_PERMITSION @GROUPROLECODE ");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@GROUPROLECODE", SqlDbType.VarChar);
            sqlParameters[0].Value = Convert.ToString(ent.GROUPUSERCODE);
            DataTable ds = conn.executeSelectQuery(query,sqlParameters);
            List<ROLEENT> List = new List<ROLEENT>();
            foreach (DataRow item in ds.Rows)
            {
                ROLEENT ROLEent = new ROLEENT();
                ROLEent.ROLECODE = item["ROLECODE"].ToString();
                ROLEent.ROLENAME = item["ROLENAME"].ToString();
                ROLEent.GROUPROLENAME = item["GROUPROLENAME"].ToString();
                ROLEent.GROUPROLECODE = item["GROUPROLECODE"].ToString();
                ROLEent.USERCREATE = item["USERCREATE"].ToString();
                ROLEent.DATECREATE = DateTime.Parse(item["DATECREATE"].ToString());
                ROLEent.USERUPDATE = item["USERUPDATE"].ToString();
                ROLEent.LASTUPDATE = DateTime.Parse(item["LASTUPDATE"].ToString());
                ROLEent.ISCHECK = bool.Parse( item["ISCHECK"].ToString());
                List.Add(ROLEent);
            }

            return List;
        }
        public bool INSERTROLE(ROLEENT ent)
        {
            string query = "exec  INSERTROLE @ROLECODE,@ROLENAME,@GROUPROLECODE,@USERCREATE";
            SqlParameter[] sqlParameters = new SqlParameter[4];
            sqlParameters[0] = new SqlParameter("@ROLECODE", SqlDbType.VarChar);
            sqlParameters[0].Value = Convert.ToString(ent.ROLECODE);
            sqlParameters[1] = new SqlParameter("@ROLENAME", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(ent.ROLENAME);

            sqlParameters[2] = new SqlParameter("@GROUPROLECODE", SqlDbType.Int);
            sqlParameters[2].Value = Convert.ToString(ent.GROUPROLECODE);
            sqlParameters[3] = new SqlParameter("@USERCREATE", SqlDbType.NVarChar);
            sqlParameters[3].Value = Convert.ToString(ent.USERCREATE);
            return conn.executeInsertQuery(query, sqlParameters);
        }
        public bool UPDATEROLEBYROLEID(ROLEENT ent)
        {
            string query = "exec  UPDATEROLEBYROLEID @ROLECODE,@ROLENAME,@GROUPROLECODE,@USERUPDATE";
            SqlParameter[] sqlParameters = new SqlParameter[4];
            sqlParameters[0] = new SqlParameter("@ROLECODE", SqlDbType.VarChar);
            sqlParameters[0].Value = Convert.ToString(ent.ROLECODE);
            sqlParameters[1] = new SqlParameter("@ROLENAME", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(ent.ROLENAME);

            sqlParameters[2] = new SqlParameter("@GROUPROLECODE", SqlDbType.Int);
            sqlParameters[2].Value = Convert.ToString(ent.GROUPROLECODE);
            sqlParameters[3] = new SqlParameter("@USERUPDATE", SqlDbType.NVarChar);
            sqlParameters[3].Value = Convert.ToString(ent.USERUPDATE);
            return conn.executeUpdateQuery(query, sqlParameters);
        }
        public bool DELTELEROLEBYROLEID(ROLEENT ent)
        {
            string query = "exec  DELTELEROLEBYROLEID @ROLECODE";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@ROLECODE", SqlDbType.VarChar);
            sqlParameters[0].Value = Convert.ToString(ent.ROLECODE);
            return conn.executeDeleteQuery(query, sqlParameters);
        }
    }
}
